<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>计算购物车总价案例</title>
</head>

<body>
  <script>
    // ============================== 计算购物车总价案例 ==============================
    let cartGoods = [
      {
        id: 1,
        name: '海飞丝',
        price: 28,
        count: 2
      },
      {
        id: 2,
        name: '电吹风',
        price: 299,
        count: 1
      },
      {
        id: 3,
        name: '卫龙大辣片',
        price: 5,
        count: 18
      },
    ]

    // 语法： arr.reduce(function(prev, item){}, 初始值)
    // 在该案例中，起始值必须要写，不然的话，prev一开始是数组的第一项（对象）
      const total = cartGoods.reduce((prev,item)=> prev + (item.price * item.count),0)
      console.log(total)

  </script>
</body>

</html>